package com.yb.module.system.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yb.framework.core.base.BaseMapper;
import com.yb.framework.core.base.BaseServiceImpl;
import com.yb.module.system.domain.SysMenu;
import com.yb.module.system.mapper.SysMenuMapper;
import com.yb.module.system.service.SysMenuService;

@Service
public class SysMenuServiceImpl extends BaseServiceImpl<SysMenu> implements SysMenuService {
	
	@Autowired
	private SysMenuMapper sysMenuMapper;
	
	@Override
	protected BaseMapper<SysMenu> getMapper() {
		return sysMenuMapper;
	}

	@Override
	public List<SysMenu> getListByPid(Long pid) {
		// TODO Auto-generated method stub
		SysMenu sysMenu = new SysMenu();
		sysMenu.setPid(pid);
		return sysMenuMapper.getListByEntity(sysMenu);
	}

	@Override
	public List<SysMenu> getListByLevel(Integer level) {
		// TODO Auto-generated method stub
		SysMenu sysMenu = new SysMenu();
		sysMenu.setLevel(level);
		return sysMenuMapper.getListByEntity(sysMenu);
	}

	@Override
	public List<SysMenu> getListByPermissions(List<String> permissions) {
		// TODO Auto-generated method stub
		return sysMenuMapper.getListByPermissions(permissions);
	}

	@Override
	public List<SysMenu> getListByParamMap(Long pid, Integer level) {
		// TODO Auto-generated method stub
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("pid", pid);
		paramMap.put("level", level);
		return sysMenuMapper.getListByParamMap(paramMap);
	}

	@Override
	public List<SysMenu> getListByParamMap(Long pid, Integer level, List<String> permissions) {
		// TODO Auto-generated method stub
		List<SysMenu> sysMenuList = new ArrayList<SysMenu>();
		if(permissions != null && permissions.size() > 0) {
			Map<String, Object> paramMap = new HashMap<String, Object>();
			paramMap.put("pid", pid);
			paramMap.put("level", level);
			paramMap.put("permissions", permissions);
			sysMenuList = sysMenuMapper.getListByParamMap(paramMap);
		}
		return sysMenuList;
	}

	@Override
	public Long getParentId(Long id) {
		// TODO Auto-generated method stub
		return sysMenuMapper.getParentId(id);
	}

	@Override
	public List<Long> getChildIds(Long id) {
		// TODO Auto-generated method stub
		return sysMenuMapper.getChildIds(id);
	}

	@Override
	public List<Long> getAllParentIds(Long id) {
		// TODO Auto-generated method stub
		return getAllParentIds(id, new ArrayList<Long>());
	}

	@Override
	public List<Long> getAllChildIds(Long id) {
		// TODO Auto-generated method stub
		return getAllChildIds(id, new ArrayList<Long>());
	}
	
	private List<Long> getAllParentIds(Long id, List<Long> parentIds) {
		Long pId = sysMenuMapper.getParentId(id);
		if(pId != null) {
			parentIds.add(pId);
			parentIds = getAllParentIds(pId, parentIds);
		}
		return parentIds;
	}
	
	private List<Long> getAllChildIds(Long id, List<Long> childIds) {
		List<Long> cIds = sysMenuMapper.getChildIds(id);
		if(cIds != null && cIds.size() > 0) {
			childIds.addAll(cIds);
			for (Long cId : cIds) {
				childIds = getAllChildIds(cId, childIds);
			}
		}
		return childIds;
	}
}